package com.zzuli.dcxt.controller;

import com.zzuli.dcxt.dao.AdminMapper;
import com.zzuli.dcxt.dao.CustomerMapper;
import com.zzuli.dcxt.entiy.Admin;
import com.zzuli.dcxt.entiy.Customer;
import com.zzuli.dcxt.entiy.Dishes;
import com.zzuli.dcxt.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @RequestMapping("/login")
    public String login(Customer customer , HttpSession session){
        Customer login = customerMapper.login(customer);
        session.setAttribute("customer",customer);
        if (login!=null){
            return "index";
        }
        return "error";
    }

    @RequestMapping("/adminLogin")
    public String adminLogin(Admin admin , HttpSession session){
        Admin adminLogin = adminMapper.login(admin);
        session.setAttribute("adminLogin",adminLogin);
        if (adminLogin!=null){
            return "index";
        }
        return "error";
    }


    @RequestMapping("/adminlist")
    public String adminlist(Model model){
        List list = adminService.getAdminList();
        model.addAttribute("list",list);
        return "admin";
    }

    @RequestMapping("/deladmin")
    public String deladmin(int id){
        adminMapper.deladmin(id);
        return "redirect:/adminlist.do";
    }

    @RequestMapping("/updateAdmin")
    public String updateAdmin(Admin admin){
        adminMapper.updateAdmin(admin);
        return "redirect:/adminlist.do";
    }

    @RequestMapping("/selectOneAdmin")
    public String selectOneAdmin(int id, HttpSession session){
        Admin admin = adminMapper.selectOneAdmin(id);
        session.setAttribute("updateAdmin",admin);
        return "updateadmin";
    }


}
